//写入数据
// db 数据库
// storename 仓库
// data 数据（对象类型）
function addData(db,storename,data){
    console.log("写入数据：",data);
    // 创建一个事务
    let transaction = db.transaction([storename], 'readwrite');
    // 获取对象仓库
    let objectStore = transaction.objectStore(storename);
    // 添加数据
    let request = objectStore.add(data);

    request.onsuccess = function (event) {
        console.log("数据写入成功");
    }

    request.onerror = function (event) {
        console.log('数据写入失败');
    }
}

//通过主键读取数据
// db 数据库实例
// storename 仓库名称
// key 主键值
function getDataByKey(db,storename,key){
    let transaction = db.transaction([storename]); //事务
    let objectStore = transaction.objectStore(storename); //仓库对象
    let request = objectStore.get(key);

    request.onerror = function (event){
        console.log("事务失败");
    }

    request.onsuccess = function (event) {
        console.log("主键查询结果：",request.result);
        return request.result;
    }
}

//通过游标查询数据
// db 数据库实例
// storename 仓库名称
function cursorGetData(db,storename) {
    let list = [];
    let transaction = db.transaction([storename],'readwrite'); //事务
    let objectStore = transaction.objectStore(storename); //仓库对象
    let request = objectStore.openCursor(); //指针对象

    //游标开启成功，逐行读取数据
    request.onsuccess = function (event) {
        var cursor = event.target.result;
        if(cursor){
            list.push(cursor.value);
            cursor.continue();
            $("#msg_con").append("<p>"+cursor.value.username+":"+cursor.value.msg+"</p>")
        }else{
            console.log('通过游标查询结果',list);
            return list;
        }
    }
}

//通过索引读取数据
// db 数据库实例
// storename 仓库名称
// indexname 索引名称
// indexvalue 索引值
function getDataByIndex(db,storename,indexname,indexvalue){
    let transaction = db.transaction([storename],'readwrite'); //事务
    let objectStore = transaction.objectStore(storename); //仓库对象
    let request = objectStore.index(indexname).get(indexvalue);

    request.onerror = function () {
        console.log("事务失败");
    };
    request.onsuccess = function (e) {
        let result = e.target.result;
        console.log("索引查询结果：", result);
        return result;
    };
}

//通过游标和索引查询数据
function cursorGetDataByIndex(db, storename, indexName, indexValue) {
    let list = [];
    let transaction = db.transaction([storename],'readonly'); //事务
    let objectStore = transaction.objectStore(storename); //仓库对象
    // 使用索引创建一个游标请求
    let index = objectStore.index(indexName);
    let request = index.openCursor(IDBKeyRange.only(indexValue));

    request.onsuccess = function (e) {
        let cursor = e.target.result;
        if (cursor) {
            // 必须要检查
            list.push(cursor.value);
            cursor.continue(); // 遍历了存储对象中的所有内容
            console.log(cursor.value.msg);
            $("#msg_con").append("<p>"+cursor.value.username+":"+cursor.value.msg+"</p>")
        } else {
            console.log("通过游标和索引查询结果：", list);
        }
    };
    request.onerror = function (e) {
        console.log('Error querying data:', event.target.errorCode);
    };
}